// index.js
// 获取应用实例

const app = getApp()
const {
  Http
} = require("../../utils/http")
const {
  globalData
} = getApp()

Page({
  data: {
    active: 0,
    motto: '礼貌问候',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
    //轮播图数据
    swiperList: [],
    //公告数据
    noticeList: [],
    //每日上新数据
    newShopList: [],
    //首页所有数据
    homeLits: []
  },
  onChange(event) {
    // event.detail 的值为当前选中项的索引
    this.setData({
      active: event.detail
    });
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    let token = wx.getStorageSync('token')
    token = token ? token : ""
    if (!token) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    // console.log(Http)
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
   
    //公告数据请求
    Http("/shop/notice/topNoticeList", {
      a: 1
    }, "GET", (res) => {
      // console.log(res)
      this.setData({
        noticeList: res.data
      })
    })
    //每日新数据请求
    Http("/prod/lastedProdPage", {
      a: 1
    }, "GET", (res) => {
      // console.log(res)
      this.setData({
        newShopList: res.data.records
      })
    })

  },

  onShow() {
    //获取首页所有数据
    Http("/prod/tagProdList", {
      a: 1
    }, "GET", (res) => {
      console.log(res.data)
      this.setData({
        homeLits: res.data
      })
    })

     //轮播图数据请求
     Http("/indexImgs", {
      a: 1
    }, "GET", (res) => {
      // console.log(res)
      this.setData({
        swiperList: res.data
      })
      console.log(this.data.swiperList)
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  //跳转详情页
  onDetail(e) {
    let prodId = e.currentTarget.dataset.id
    //商品id存到全局变量中
    globalData.prodId = prodId
    console.log(prodId)
    wx.navigateTo({
      // url: `/pages/detail/detail?prodId=${prodId}`,
      url: `/pages/detail/detail`,
      // url:' /pages/detail/detail.js?prodId'+prodId,
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onShareAppMessage() {
    return {
      title: 'swiper',
      path: 'page/component/pages/swiper/swiper'
    }
  },

  data: {
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    indicatorDots: false,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500
  },

  changeIndicatorDots() {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },

  changeAutoplay() {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },

  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },

  durationChange(e) {
    this.setData({
      duration: e.detail.value
    })
  }
})